<template>
  <div class="t-sell" avalonctrl="vmPos">
    <table cellspacing="0" class="list-wrap " style="font-size: 14px">
      <tbody>
        <tr>
          <th style="width: 280px" class="pl10">创建时间</th>
          <th>合约摘要</th>
          <th style="width: 80px">成本价</th>
          <th style="width: 80px">当前价</th>
          <th style="width: 80px">浮动盈亏</th>
          <th style="width: 80px;">倍数</th>
          <th style="width: 120px">操作</th>
        </tr>

        <tr v-for="(item,index) in nowShare" :key="index">
          <td class="pl10">{{item.created_at}}<br>
            <!-- <span>合约ID：</span> <span class="txt-up">20181203000038</span> -->
          </td>
          <td>
            <span class="txt-up" style="display: none;">正在创建策略，请稍候......</span>
            <span class="txt-up" style="display: none;">正在策略，请稍候......</span>
            <br style="display: none;">
            {{item.name}}（{{item.code}}） <span class="txt-up">{{item.num}} </span>股<br>

            信用金：{{item.count_xingyongjin}} 元                        
          </td>
          <td>
            <span class="currprice ">{{item.price}}</span>
            <span class="currprice " style="display: none;">------</span>
          </td>
          <td>
            <span class="currprice ">{{item.current_price}}</span>
            <span class="driftprice " style="display: none;">------</span>
          </td>
          <td>
            <span class="driftprice txt-up">{{item.float_earn}}</span>
            <span class="driftprice " style="display: none;">------</span>
          </td>
          <td>
            <span class="driftprice ">{{item.multiple}}</span>
            <span class="driftprice " style="display: none;">------</span>
          </td>
          <td>
            <span class="txt-up" style="display: none;">创建策略中......</span>
            <span class="txt-up" style="display: none;">策略中......</span>

            <button class="btn" @click="sell(item.tactic_id)" title="立即卖出">卖出</button>
            <button @click="add(item.tactic_id)" class="btn" style="margin-top: 5px;">追加</button>
          </td>
        </tr>

      </tbody>
    </table>
    <div style="padding-top: 150px; text-align: center; display: none;">
      <p class="pb20 ">暂时没有等待卖出的A股</p>
      <p>
        <button style="display: block; width: 200px; height: 40px; text-align: center; color: #fff; border: none; background: url(../newImages/btn-bg.png) no-repeat!important; border-radius: 20px; margin: 0 auto;">立即去创建策略</button>
      </p>
    </div>
    <div class="pagination clearfix " style="">
      <ul class="clearfix " style="margin-right: 0">
        <li>
          <a @click="fy(-1)" style="transform:scale(-1)" title="上一页" class="page prev iconfont">&#xe611;</a>
        </li>

        <li style="line-height: 10px; height: 30px; vertical-align: middle">记录总数：<i style="color: red">{{all}}</i> &nbsp;&nbsp;页码： <i style="color: red">{{now}}</i>&nbsp;/&nbsp;{{last}}
        </li>
        <li>
          <a @click="fy(1)" title="下一页" class="page next iconfont">&#xe611;</a>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import Axios from "@/common/js/Axios";
export default {
  mounted() {
    this.getNow(this.now);
  },
  data() {
    return {
      token: localStorage.getItem("Authorization"),
      //当前持仓数组
      nowShare: [],
      //总信息
      all:1,
      //当前页码
      now:1,
      // 总共页码
      last:1
    };
  },
  methods: {
    //翻页
    fy(v){      
      if(v == -1){
        this.getNow(--this.now);
      }else{
        this.getNow(++this.now);
      }
    },
    // 追加信用金
    async add(id){
      let ok = true;
      let mvalue = 0;
      await this.$prompt('请输入追加金额', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
        }).then(({ value }) => {
          mvalue = value
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消追加'
          });       
          ok = false;
      });
      if(!ok){
        return false;
      }
      Axios.postAxios(
        '/api/tactics/addshoudong',
        {
          tactic_id : id,
          xingyongjin : mvalue
        },
        {
          Authorization: "Bearer " + this.token
        }
      ).then(res=>{     
          console.log(res);
          if(!res.status){
            this.$message.error(res.data.msg);
          }else{            
            this.$message.success(res.data.msg);            
            this.getNow(this.now);
          }
      },err=>{
        console.log(err);
        this.$message.error('添加失败');
      })
    },
    //卖出当前股票
    async sell(code) {
      let ok = true;
      await this.$confirm("确认卖出此策略吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {})
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消卖出"
          });
          ok = false;
        });
      if (!ok) {
        return false;
      }
      Axios.postAxios(
        "/api/tactics/sale",
        {
          tactic_id: code
        },
        {
          Authorization: "Bearer " + this.token
        }
      ).then(
        res => {
          if (res.data.status == false) {
            this.$message.error(res.data.msg);
          } else {
            this.$message.success(res.data.msg);
            this.getNow(this.now);
          }
        },
        err => {
          this.$message.error("卖出失败");
        }
      );
    },
    //获取当前持仓
    getNow(page) {
      if(page<1){        
        this.now = 1;
        return false;
      }
      if(page>this.last){
        this.now=  this.last;
        return false;
      }
      Axios.getAxios(
        "/api/strategyChiCangPc/normal",
        {
          page:page
        },
        {
          Authorization: "Bearer " + this.token
        }
      ).then(res => {
        console.log(res);
        this.nowShare = res.data.data.data;
        this.all = res.data.data.total;
        this.now = res.data.data.current_page;
        this.last = res.data.data.last_page;
      });
    }
  }
};
</script>

<style lang="stylus" scoped>
@import '~@/common/css/pager';
.list-wrap th {
  border: none;
  height: 40px;
  background-color: #E9E9E9;
  font-weight: bold;
  text-align: left;
  padding-left: 10px;
}
.list-wrap td {
  padding-top: 15px;
  padding-bottom: 15px;
  border-bottom: 1px solid #DBDBDB;
}
.iconfont {
  font-size: 20px !important;
  border: none !important;
  cursor: pointer;
}
</style>

